package org.liaohailong.helloworld;

import org.junit.Test;
import org.liaohailong.helloworld.example.FT;
import org.liaohailong.helloworld.example.GG;
import org.liaohailong.helloworld.example.SortTest;
import org.liaohailong.helloworld.sort.CountSort;
import org.liaohailong.helloworld.sort.ISort;
import org.liaohailong.helloworld.sort.QuickSort;
import org.liaohailong.helloworld.sort.RadixSort;

import java.util.Arrays;

/**
 * Author: liaohailong
 * Time: 2021/8/2 16:12
 * Describe: 分类测试用例
 */
public class SortExam implements AUtil.ILog {

    @Test
    public void radixSort() {
        ISort sort = new RadixSort();
        int[] arr = {456, 654, 987, 789, 78, 87, 89, 98, 65, 56, 45, 54, 12, 21, 32, 12, 999};
        sort.sort(arr);
        log(Arrays.toString(arr));

//        ClassLayout layout = ClassLayout.parseInstance("abcde");
//        log(layout.toPrintable());

//        int i = Runtime.getRuntime().availableProcessors();
//        log("cpu count = " + i);
    }

    @Test
    public void countSort() {
        ISort sort = new CountSort();
        int[] arr = {1, 2, 3, 4, 6, 8, 7, 6, 4, 5, 5, 5, 5, 5, 3, 2, 2, 3, 4, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
        sort.sort(arr);
        log(Arrays.toString(arr));
    }

    @Test
    public void quickSort() {
        ISort sort = new QuickSort();
//        int[] arr = new int[100];
//        for (int i = 0; i < 100; i++) arr[i] = i;
//        LinkedList<Integer> list = new LinkedList<>();
//        for (int i : arr) list.add(i);
//        Collections.shuffle(list);
//        int pos = 0;
//        for (int value : list) {
//            arr[pos] = value;
//            pos++;
//        }
//        log(Arrays.toString(arr));
        int[] arr = {1, 7, 2, 8, 3, 9, 4, 0, 5, 6};
        sort.sort(arr);
        log(Arrays.toString(arr));
    }


    @Test
    public void deathLock() {
        final Object obj_1 = new Object(), obj_2 = new Object();

        Thread t1 = new Thread("t1") {
            @Override
            public void run() {
                synchronized (obj_1) {
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                    }

                    synchronized (obj_2) {
                        System.out.println("thread t1 done.");
                    }
                }
            }
        };

        Thread t2 = new Thread("t2") {
            @Override
            public void run() {
                synchronized (obj_2) {
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                    }

                    synchronized (obj_1) {
                        System.out.println("thread t2 done.");
                    }
                }
            }
        };

        t1.start();
        t2.start();
    }

    @Test
    public void test() {
        SortTest.main(new String[]{""});
    }

    @Test
    public void minRefuelStops() {
        AUtil.setLog(this);
        FT ft = new FT();
//        输入：target = 1, startFuel = 1, stations = []
//        输出：0
//        解释：我们可以在不加油的情况下到达目的地。
//        int target = 1;
//        int startFuel = 1;
//        int[][] stations = new int[0][0];

//        输入：target = 100, startFuel = 1, stations = [[10,100]]
//        输出：-1
//        解释：我们无法抵达目的地，甚至无法到达第一个加油站。
//        int target = 100;
//        int startFuel = 1;
//        int[][] stations = {{10, 100}};

//        输入：target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]]
//        输出：2
//        解释：
//        我们出发时有 10 升燃料。
//        我们开车来到距起点 10 英里处的加油站，消耗 10 升燃料。将汽油从 0 升加到 60 升。
//        然后，我们从 10 英里处的加油站开到 60 英里处的加油站（消耗 50 升燃料），
//        并将汽油从 10 升加到 50 升。然后我们开车抵达目的地。
//        我们沿途在1两个加油站停靠，所以返回 2 。
//        int target = 100;
//        int startFuel = 10;
//        int[][] stations = {{10, 60}, {20, 30}, {30, 30}, {60, 40}};

//        int target = 100;
//        int startFuel = 50;
//        int[][] stations = {{50, 50}};


//        int target = 1000;
//        int startFuel = 83;
//        int[][] stations = {{25, 27}, {36, 187}, {140, 186}, {378, 6}, {492, 202}, {517, 89}, {579, 234}, {673, 86}, {808, 53}, {954, 49}};


        int target = 1000;
        int startFuel = 75;
        int[][] stations = {{12,173},{53,111},{104,9},{140,68},{179,148},{268,42},{301,161},{324,123},{378,129},{400,179},{408,183},{409,64},{454,28},{511,140},{579,150},{586,55},{680,82},{701,171},{702,34},{707,20},{860,188},{907,3},{935,43},{955,5},{977,91}};

        AUtil.printCostInMs(() -> {
            int i = ft.minRefuelStops(target, startFuel, stations);
            log("minRefuelStops = " + i);
        }, "minRefuelStops");
    }

    @Test
    public void stringCombined(){
        GG.checkIsCombined();
    }

    public void log(String msg) {
        log("SortExam", msg);
    }

    @Override
    public void log(String tag, String msg) {
        System.out.println(tag + ": " + msg);
    }
}
